package com.yidu.boss.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.boss.sys.domain.Broker;
import com.yidu.boss.sys.service.BrokerService;
import com.yidu.boss.util.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: fund_management_system
 * @ClassName BrokerController
 * @author: Zc
 * @create: 2019-11-26 09:00
 **/

@Controller
@RequestMapping("/broker/")
public class BrokerController {
    @Autowired
    BrokerService brokerService;


    @RequestMapping("broker")
    public String broker(){
        return "broker";
    }


    @RequestMapping("selectAll")
    @ResponseBody
    public Map selectAll(Integer page,Integer limit){
        //页面大小，页面
        PageHelper.startPage(page,limit);
        //根据条件查询管理人数据
        List<Broker> custodianList=brokerService.selectAll();
        //使用pageInfo包装查询后的结果，封装了详细的查询数据，其中参数是列表数据
        PageInfo<Broker> custodianPageInfo =new PageInfo<Broker>(custodianList);
        //列表数据
        List<Broker> custodianData =custodianPageInfo.getList();
        //map集合
        Map<String,Object> map =new HashMap<String,Object>();
        //分页查询编码从0开始
        map.put("code", 0);
        //信息
        map.put("msg", "");
        //分页统计数量
        map.put("count",custodianPageInfo.getTotal());
        //分页数据
        map.put("data", custodianData);
        return map;
    }


    @RequestMapping("delete")
    @ResponseBody
    public String delete(String[] ids){
        int i=0;
        for (String id : ids) {
             i += brokerService.delete(id);
        }
        if (i>0){
            return "删除成功";
        }
        return "删除失败";

    }

    @RequestMapping("add")
    @ResponseBody
    public String add(Broker broker){
        int i = brokerService.add(broker);

        if (i>0){
            return "添加成功";
        }
        return "添加失败";
    }

    @RequestMapping("updata")
    @ResponseBody
    public String updata(Broker broker){
        System.out.println("broker = " + broker);
        int i = brokerService.update(broker);
        if (i>0){
            return "修改成功";
        }
        return "修改失败";
    }

    @RequestMapping("getName")
    @ResponseBody
    public List<Broker> selectName(){
        List<Broker> brokers = brokerService.selectAll();
        return brokers;
    }
    /**
     * 模糊查询
     *
     * @param condition 模糊查询条件
     * @param page      页码
     * @param limit     数据条数
     * @return 返回前端数据格式
     */
    @RequestMapping("selectLike")
    @ResponseBody
    public Object selectLike(String condition, Integer page, Integer limit) {
        System.out.println(condition);
        //判断模糊查询条件参数是否为空
        if(condition.equals("")){
            //为空显示所有列表页面
            Object o =selectAll(page,limit);
            return o;
        }
        //开始页
        PageHelper.startPage(page,limit);
        //调用业务层方法
        List<Broker> brokerList =brokerService.selectLike(condition);
        PageInfo<Broker> brokerPageInfo =new PageInfo<Broker>(brokerList);
        //调用工具类的分页计数数据格式：map集合
        Map<String, Object> map = RandomString.pagingALG(brokerPageInfo,brokerPageInfo.getTotal());
        return map;
    }




}
